package com.gongpingjialoan.activity.fb;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.gongpingjialoan.activity.car.AgentReCommendCarActivity;
import com.gongpingjialoan.activity.car.CarBuyDetailActivity;
import com.gongpingjialoan.activity.car.CarListActivity;
import com.gongpingjialoan.activity.car.DiscountDetailActivity;
import com.gongpingjialoan.activity.car.MyLikeCarActivity;
import com.gongpingjialoan.activity.car.NewUseCarAssResultActivity;
import com.gongpingjialoan.activity.car.SellCarDetailActivity;
import com.gongpingjialoan.activity.car.SellCarHistoryActivity;
import com.gongpingjialoan.activity.main.MainActivity;
import com.gongpingjialoan.activity.main.SplashActivity;
import com.gongpingjialoan.activity.main.ValueReportActivity;
import com.gongpingjialoan.activity.sell.SellPrivateCarDetailActivity;
import com.gongpingjialoan.activity.tool.LicenceResultActivity;
import com.gongpingjialoan.activity.vip.VIPDetailActivity;
import com.gongpingjialoan.utility.Utils;
import com.xiaomi.mipush.sdk.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;

import java.util.Map;

public class MyPushIntentService extends PushMessageReceiver {
    private static final String TAG = MyPushIntentService.class.getName();

    public static final String CAR_DETAIL = "detail";
    public static final String BILLBOARD = "billboard";
    public static final String BILLBOARDTYPE = "billboardtype";
    public static final String PROMOTIONDETAIL = "promotiondetail";
    public static final String PROMINTIONLIST = "promotionlist";
    public static final String MARKET = "market";
    public static final String FEEDBACK = "feedback";
    public static final String RECKON = "reckon";
    public static final String MYCAR = "mycar";
    public static final String RECOMMEND = "carrecommend";
    public static final String CHOOSECAR = "carlist";
    public static final String VIOLATION = "violation";
    public static final String PRICECHANGE = "pricechange";
    public static final String VIPLIST = "viplist";
    public static final String CUSTOMLIST = "customlist";
    public static final String SELLCAR = "evalsellcar";
    public static final String SELLCARHISTORY = "sellcarhistory";
    public static final String SELLCARHISTORYDETAIL = "sellcarhistorydetail";
    public static final String SELLPRIVATECAR = "personalSellVerification";
    public static final String RECOMENDCARLIST = "dealerRecommendCarList";
    public static final String MESSAGECENTER = "messagecenter";
    public static final String PERSONERSEllRECONDS = "personalSellRecords";
    public static final String VIPBUY = "vipbuy";
    public static final String RECKONBUY = "reckonbuy";
    public static final String LOANRESULT = "loan_result";


    private Intent addMessageToIntent(Intent intent, MiPushMessage msg) {
        if (intent == null || msg == null || msg.getExtra() == null)
            return intent;
        Map<String, String> maps = msg.getExtra();
        for (Map.Entry<String, String> entry : maps.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (key != null)
                intent.putExtra(key, value);
        }
        return intent;
    }


    @Override
    public void onReceivePassThroughMessage(Context context, MiPushMessage message) {

    }

    @Override
    public void onNotificationMessageClicked(Context context, MiPushMessage msg) {
        Intent resultIntent = new Intent();
        resultIntent = addMessageToIntent(resultIntent, msg);
        if (!Utils.getAppSatus(context, "com.gongpingjia")) {
            resultIntent.setClass(context, SplashActivity.class);
        } else {
            String type = msg.getExtra().get("type");
            if (MyPushIntentService.CAR_DETAIL.equals(type) || MyPushIntentService.PRICECHANGE.equals(type)) {
                resultIntent.setClass(context, CarBuyDetailActivity.class);
            } else if (MyPushIntentService.SELLCAR.equals(type) || MyPushIntentService.CHOOSECAR.equals(type) || MyPushIntentService.RECOMMEND.equals(type) || MyPushIntentService.BILLBOARD.equals(type) || MyPushIntentService.BILLBOARDTYPE.equals(type) || MyPushIntentService.PROMINTIONLIST.equals(type) || MyPushIntentService.MARKET.equals(type)) {
                resultIntent.setClass(context, MainActivity.class);
            } else if (MyPushIntentService.VIPLIST.equals(type) || MyPushIntentService.CUSTOMLIST.equals(type)) {
                resultIntent.setClass(context, CarListActivity.class);
            } else if (MyPushIntentService.SELLCARHISTORYDETAIL.equals(type)) {
                resultIntent.setClass(context, SellCarDetailActivity.class);
            } else if (MyPushIntentService.PROMOTIONDETAIL.equals(type)) {
                resultIntent.setClass(context, DiscountDetailActivity.class);
            } else if (MyPushIntentService.RECKON.equals(type)) {
                resultIntent.setClass(context, ValueReportActivity.class);
//                resultIntent.putExtra("city", msg.getExtra().get("city"));
//                resultIntent.putExtra("description", msg.getExtra().get("description"));
//                resultIntent.putExtra("typevalue", msg.getExtra().get("typevalue"));

            } else if (MyPushIntentService.VIOLATION.equals(type)) {
                resultIntent.setClass(context, LicenceResultActivity.class);
            } else if (MyPushIntentService.SELLCARHISTORY.equals(type)) {
                resultIntent.setClass(context, SellCarHistoryActivity.class);
            } else if (MyPushIntentService.MYCAR.equals(type)) {
                resultIntent.setClass(context, MyLikeCarActivity.class);
            } else if (MyPushIntentService.SELLPRIVATECAR.equals(type)) {
                resultIntent.setClass(context, SellPrivateCarDetailActivity.class);
            } else if (MyPushIntentService.PERSONERSEllRECONDS.equals(type)) {
                resultIntent.setClass(context, SellCarHistoryActivity.class);
                resultIntent.putExtra("private", true);
            } else if (MyPushIntentService.RECOMENDCARLIST.equals(type)) {
                resultIntent.setClass(context, AgentReCommendCarActivity.class);
            } else if (MyPushIntentService.MESSAGECENTER.equals(type)) {
//                resultIntent.setClass(context, MessageCenterActivity.class);
            } else if (MyPushIntentService.VIPBUY.equals(type)) {
                resultIntent.setClass(context, VIPDetailActivity.class);
            } else if (MyPushIntentService.RECKONBUY.equals(type)) {
                resultIntent.setClass(context, NewUseCarAssResultActivity.class);
            } else if (MyPushIntentService.LOANRESULT.equals(type)) {
                //网络请求
                resultIntent.setClass(context, SplashActivity.class);
            }
        }
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(resultIntent);
    }


    @Override
    public void onNotificationMessageArrived(Context context, MiPushMessage message) {
        String type = message.getExtra().get("type");
        if (PERSONERSEllRECONDS.equals(type)) {
            if (Utils.getAppSatus(context, "com.gongpingjia")) {
//                context.sendBroadcast(new Intent(Constant.REGISTER_USER_DIALOG));
            }
        }
    }

    @Override
    public void onCommandResult(Context context, MiPushCommandMessage message) {
        String command = message.getCommand();
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                Log.d("push", "------------------------------------" + MiPushClient.getRegId(context));

                Utils.LogD("注册成功");
            } else {
                Utils.LogD("注册失败");
            }
        } else if (MiPushClient.COMMAND_SET_ALIAS.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                Utils.LogD("设置别名成功");
                Utils.LogD("push------------------------------------" + MiPushClient.getRegId(context));
            } else {
                Utils.LogD("设置别名失败");
            }
        }
    }


}
